#!/usr/bin/env python
import sys
from os.path import isfile

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as N


from ui_Plot_Setup_Dialog import Ui_Define_Plot_Widget
import Plot_Widget
from plotDataClass import LPC


class Define_Plot_Widget(QWidget):
    def __init__(self, array_list=None, varDict=None, append = False,  parent = None):
        QWidget.__init__(self, None)
        self.ui = Ui_Define_Plot_Widget()
        self.ui.setupUi(self)
        
        self.append = append
        self.parent = parent
        #print self.parent.dialog_test
        self.varDict = varDict
        self.initialize_setup(array_list)

        self.connect(self.ui.btn_PlotSelection, SIGNAL("clicked()"),
                     self.getListSelections)
        
    def initialize_setup(self, array_list):
        if array_list is not None:
            self.ui.x_listWidget.addItem('None')
            self.ui.x_listWidget.addItems(array_list)
            self.ui.x_listWidget.setCurrentRow(0)
            self.ui.y_listWidget.addItems(array_list)
            self.ui.y_listWidget.setCurrentRow(0)
    
    def getListSelections(self):
        x_items = self.ui.x_listWidget.selectedItems()#gets data in X ListWidget
        y_items = self.ui.y_listWidget.selectedItems()#gets data in Y ListWidget
        x_dict = {str(x_items[0].text()):self.varDict.get(str(x_items[0].text()))}
        #the x dict is constructed using the name of selected x array and 
        #the actual array itself.  This will allow it to be selected further
        #in the program by using the name as a dictionary key.
        #print "X Items: ",unicode(x_items[0].text())
        #print "Y Items: "
        y_list = []
        for item in y_items:#this loop creates a series of tuples ala (name,array) 
            y_list.append((str(item.text()),self.varDict.get(str(item.text()))))
            #print str(item.text())
            
        y_dict = dict(y_list)#make a dictionary using the tuples
        #print "X Dict", x_dict
        dataDict = [x_dict, y_dict]#acutally this is a list of dictionaries
        self.__plotData__(dataDict)
    
    def __plotData__(self, data2plot):
        if self.append is False:
            self.plot_dialog = Plot_Widget(LPC(data2plot),self.parent)
            self.plot_dialog.show()
            self.close()
        else:
            self.parent.add_trace(data2plot)
            self.close()
        
    
    
    def program_close(self):
        sys.exit()

    
if __name__ == "__main__":
    array_list = ["Banana", "Apple", "Elderberry", "Clementine", "Fig",
             "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon",
             "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi",
             "Lemon", "Nectarine", "Plum", "Raspberry", "Strawberry",
             "Orange"]
            
    app = QApplication(sys.argv)
    plot_setup = Define_Plot_Widget(array_list)
    plot_setup.show()
    sys.exit(app.exec_())

